package com.fangwaii.core.socket;

import com.fangwaii.core.session.Configuration;
import com.fangwaii.core.session.defaults.DefaultGatewaySessionFactory;
import com.fangwaii.core.socket.handlers.GatewayServerHandler;
import com.fangwaii.core.socket.handlers.AuthorizationHandler;
import com.fangwaii.core.socket.handlers.DefaultHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * @author lxc18
 * @date 2024/11/29 11:35
 * @description SessionChannelinitializer
 */
public class GatewayChannelInitializer extends ChannelInitializer<SocketChannel> {
    private final DefaultGatewaySessionFactory gatewaySessionFactory;
    private final Configuration configuration;

    public GatewayChannelInitializer(Configuration configuration,DefaultGatewaySessionFactory gatewaySessionFactory) {
        this.gatewaySessionFactory = gatewaySessionFactory;

        this.configuration = configuration;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new HttpRequestDecoder());
        pipeline.addLast(new HttpResponseEncoder());
        pipeline.addLast(new HttpObjectAggregator(1024*1024));
        pipeline.addLast(new DefaultHandler(configuration));
        pipeline.addLast(new AuthorizationHandler(configuration));//这两个是不是能合并成一个？
        pipeline.addLast(new GatewayServerHandler(gatewaySessionFactory));
    }
}
